package com.edu.buservice.biz.impl;

import com.edu.buservice.biz.RoleManger;
import com.edu.buservice.dal.model.SysRoles;
import com.edu.buservice.dal.model.SysUserRoles;
import com.edu.buservice.dal.service.SysRolesService;
import com.edu.buservice.dal.service.SysUserRolesService;
import com.edu.buservice.util.model.enums.ErrorCodeEnum;
import com.edu.buservice.util.model.error.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-02-09 18:39
 **/

@Component
public class RoleMangerImpl implements RoleManger {

	@Autowired
	private SysRolesService sysRolesService;

	@Autowired
	private SysUserRolesService sysUserRolesService;


	@Override
	public SysRoles getRolesByUserId(String userId) {

		SysUserRoles rolesByUserId = sysUserRolesService.getRolesByUserId(userId);

		if(Objects.isNull(rolesByUserId)){
			throw new BizException(ErrorCodeEnum.ROLE_NOT_GRANTED);
		}

		SysRoles sysRole = sysRolesService.getById(rolesByUserId.getRoleId());

		if(Objects.isNull(sysRole)){
			throw new BizException(ErrorCodeEnum.ROLE_NOT_GRANTED);
		}


		return sysRole;
	}

	@Override
	public void addUserRole(String userId, String roleId) {

		sysUserRolesService.addUserRole(userId, roleId);

	}
}
